//
//  MyConcernConfigService.m
//  homeland
//
//  Created by SunShine on 13-10-20.
//  Copyright (c) 2013年 xzysun. All rights reserved.
//

#import "MyConcernConfigService.h"
#import "DataStoreService.h"
#import "UserInfoService.h"

@interface MyConcernConfigService ()
//@property (assign, nonatomic) NSInteger currentSelectSegment;
@end

@implementation MyConcernConfigService

static MyConcernConfigService *singlton;
+(MyConcernConfigService *)getService
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singlton = [[MyConcernConfigService alloc] init];
        [singlton initValue];
        [[NSNotificationCenter defaultCenter] addObserver:singlton selector:@selector(initValue) name:LOGOUT_NOTIFICATION_NAME object:nil];
    });
    return singlton;
}

-(void)initValue
{
    //初始化数组
    self.selectNoticeStreetType = [NSMutableArray array];
    self.selectNoticeUnitType = [NSMutableArray array];
    self.selectNoticeCategoryType = [NSMutableArray array];
    self.selectFileRuleCategoryType = [NSMutableArray array];
    //通知类别和文件类型是写死的
    self.noticeCategoryTypeList = [NSArray arrayWithObjects:@"招标通告", @"活动预告", @"人事招考", @"其他通知", nil];
    self.fileRuleCategoryTypeList = [NSArray arrayWithObjects:@"区政府文件", @"区政府办文件", nil];
    UserInfoService *userService = [UserInfoService getUserInfoService];
    NSString *typeImportantNews;
    NSString *typeNoticeStreet;
    NSString *typeNoticeUnit;
    NSString *typeNoticeCategory;
    NSString *typeFileRuleCategory;
    if (userService.currentLoginUserName) {
        //已经登录，读取登录数据并覆盖到本地
        typeImportantNews = [NSString stringWithFormat:@"%d",userService.userCustomInfo.typeSceneCategory.integerValue];
        [DataStoreService saveToKeychain:LOCAL_IMPORTANT_NEWS_TYPE_KEY data:typeImportantNews];
        typeNoticeStreet = userService.userCustomInfo.typeNoticeStreet;
        [DataStoreService saveToKeychain:LOCAL_NOTICE_STREET_TYPE_KEY data:typeNoticeStreet];
        typeNoticeUnit = userService.userCustomInfo.typeNoticeUnit;
        [DataStoreService saveToKeychain:LOCAL_NOTICE_UNIT_TYPE_KEY data:typeNoticeUnit];
        typeNoticeCategory = userService.userCustomInfo.typeNoticeCategory;
        [DataStoreService saveToKeychain:LOCAL_NOTICE_CATEGORY_TYPE_KEY data:typeNoticeCategory];
        typeFileRuleCategory = userService.userCustomInfo.typeRuleCategory;
        [DataStoreService saveToKeychain:LOCAL_FILE_RULE_CATEGORY_TYPE_LIST data:typeFileRuleCategory];
    } else {
        //没登录，读取本地数据
        typeImportantNews = [DataStoreService loadFromKeychain:LOCAL_IMPORTANT_NEWS_TYPE_KEY];
        typeNoticeStreet = [DataStoreService loadFromKeychain:LOCAL_NOTICE_STREET_TYPE_KEY];
        typeNoticeUnit = [DataStoreService loadFromKeychain:LOCAL_NOTICE_UNIT_TYPE_KEY];
        typeNoticeCategory = [DataStoreService loadFromKeychain:LOCAL_NOTICE_CATEGORY_TYPE_KEY];
        typeFileRuleCategory = [DataStoreService loadFromKeychain:LOCAL_FILE_RULE_CATEGORY_TYPE_LIST];
    }
    //读取要情动态配置
    if (typeImportantNews && typeImportantNews.length == 1) {
        self.selectSceneCategoryType = typeImportantNews.integerValue;
    }
    //读取街道配置
    if (typeNoticeStreet && typeNoticeStreet.length > 0) {
        NSArray *tempArray = [typeNoticeStreet componentsSeparatedByString:@","];
        [self.selectNoticeStreetType addObjectsFromArray:tempArray];
    }
    //读取部门配置
    if (typeNoticeUnit && typeNoticeUnit.length > 0) {
        NSArray *tempArray = [typeNoticeUnit componentsSeparatedByString:@","];
        [self.selectNoticeUnitType addObjectsFromArray:tempArray];
    }
    //读取类别配置
    if (typeNoticeCategory && typeNoticeCategory.length > 0) {
        NSArray *tempArray = [typeNoticeCategory componentsSeparatedByString:@","];
        [self.selectNoticeCategoryType addObjectsFromArray:tempArray];
    }
    //读取政策文件配置
    if (typeFileRuleCategory && typeFileRuleCategory.length > 0) {
        NSArray *tempArray = [typeFileRuleCategory componentsSeparatedByString:@","];
        [self.selectFileRuleCategoryType addObjectsFromArray:tempArray];
    }
}

//-(void)selectSegmentBarAtIndex:(NSInteger)selected
//{
//    self.currentSelectSegment = selected;
//}

-(NSMutableArray *)getCurrentSelectList
{
    if (_currentConfigType == MyConcernConfigTypeNotice) {
//        if (_currentSelectSegment == 0) {
//            return _selectNoticeStreetType;
//        }
//        if (_currentSelectSegment == 1) {
//            return _selectNoticeUnitType;
//        }
//        if (_currentSelectSegment == 2) {
//            return _selectNoticeCategoryType;
//        }
        return _selectFileRuleCategoryType;
    } else if (_currentConfigType == MyConcernConfigTypeFile) {
        return  _selectFileRuleCategoryType;
    }
    return nil;
}

-(void)setCurrentConfigType:(MyConcernConfigType)currentConfigType
{
    _currentConfigType = currentConfigType;
    if (currentConfigType == MyConcernConfigTypeFile) {
        self.dataList = _fileRuleCategoryTypeList;
    } else {
        self.dataList = _noticeStreetTypeList;
    }
}

-(void)loadDataWithKey:(NSString *)keyOrNil
{
    self.dataList = nil;
    if (_currentConfigType == MyConcernConfigTypeFile) {
        //政策文件直接使用本地列表
        self.dataList = _fileRuleCategoryTypeList;
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFinished)]) {
            [self.delegate loadDataFinished];
        }
    } else if (_currentConfigType == MyConcernConfigTypeNotice) {
//        if (_currentSelectSegment == 2) {
        //类别直接使用本地列表
        self.dataList = _noticeCategoryTypeList;
        if (self.delegate && [self.delegate respondsToSelector:@selector(loadDataFinished)]) {
            [self.delegate loadDataFinished];
        }
//        } else {
//            NSMutableDictionary *params = [NSMutableDictionary dictionary];
//            if (_currentSelectSegment == 0) {
//                //街道
//                _dataItemName = @"StreetInfoItem";
//                [params setValue:@"2" forKey:@"item_type"];
//                [self doHttpPost:STREETSLIST headers:nil path:@"" parameters:params];
//            } else if (_currentSelectSegment == 1) {
//                //部门
//                _dataItemName = @"UnitInfoItem";
//                [params setValue:@"1" forKey:@"item_type"];
//                [self doHttpPost:STREETSLIST headers:nil path:@"" parameters:params];
//            }
//        }
    }
}
@end
